package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mannortycoon.dao.SeedDao;
import com.example.mannortycoon.entity.CropEntity;
import com.example.mannortycoon.entity.SeedEntity;
import com.example.mannortycoon.service.CropService;
import com.example.mannortycoon.service.SeedService;
import com.example.mannortycoon.service.SeedUnlockConditionsService;
import com.example.mannortycoon.service.UserSeedService;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.Query;
import com.example.mannortycoon.vo.SeedVo;
import com.example.mannortycoon.vo.UnlockConditionInfo2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SeedServiceImpl extends ServiceImpl<SeedDao, SeedEntity> implements SeedService {

    private final SeedUnlockConditionsService seedUnlockConditionsService;

    private final CropService cropService;

    @Autowired
    @Lazy
    private UserSeedService userSeedService;

    @Autowired
    public SeedServiceImpl(SeedUnlockConditionsService seedUnlockConditionsService,
                           CropService cropService){
        this.seedUnlockConditionsService = seedUnlockConditionsService;
        this.cropService = cropService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeedEntity> page = this.page(
                new Query<SeedEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<SeedVo> getSeedList(Long userId) {
        QueryWrapper<SeedEntity> seedEntityQueryWrapper = new QueryWrapper<>();
        seedEntityQueryWrapper.eq("is_sold",1);
        List<SeedEntity> list = this.list(seedEntityQueryWrapper);
        return list.stream().map(item->{
            SeedVo seedVo = new SeedVo();
            BeanUtils.copyProperties(item,seedVo);
            List<UnlockConditionInfo2> unlockConditionInfo2s = new ArrayList<>();
            //查询用户是否有该种子，如果有则不添加解锁条件
            if(!userSeedService.has(userId,item.getSeedId())){
                unlockConditionInfo2s = seedUnlockConditionsService.getUnlockSeedsWithUser(item.getSeedId(),userId);
            }
            seedVo.setUnlockConditionInfos(unlockConditionInfo2s);
            return seedVo;
        }).collect(Collectors.toList());
    }

    @Override
    public void saveSeed(SeedEntity seedEntity) {
        CropEntity cropEntity = new CropEntity();
        cropEntity.setCropName(seedEntity.getSeedName());
        cropEntity.setCropImgUrl(seedEntity.getSeedImgUrl());
        cropEntity.setCreateTime(new Date());
        cropService.save(cropEntity);
        seedEntity.setCropId(cropEntity.getCropId());
        this.save(seedEntity);
    }
}
